import Vue from 'vue'
// 引入子组件
import Login from '@/components/login/login'

import Home from '@/components/home/home'

import Router from 'vue-router'
Vue.use(Router)
// 创建一个路由对象并且暴露给外界
var router = new Router({
  routes: [
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    // 如果请求的是login可以访问
    next()
  } else {
    // 请求的不是login， 就是判断有没有token
    var token = window.localStorage.getItem('token')
    // 判断
    if (token) {
      next()
    } else {
      // this.$router.push('/login')
      window.location = 'http://localhost:8080/#/login'
    }
  }
})

export default router
